//
//  SinaOriginalView.swift
//  weibo
//
//  Created by sirius on 16/1/6.
//  Copyright © 2016年 sirius. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class SinaOriginalView: UIView {

     //定义一个1全局的约束来记录
    var picConstaint: Constraint?
  
    var originalViewModel: SinaStatusViewModel?{
        didSet{
            //昵称赋值
            nameLabel.text = originalViewModel?.model?.user?.screen_name
            photoImageView.sd_setImageWithURL(originalViewModel?.profile_url)
            verifitiedImageView.image = originalViewModel?.verifited_Image
            levelImageView.image = originalViewModel?.vipLevel_image
            contentLabel.text = originalViewModel?.model?.text
            sourceLabel.text = originalViewModel?.source_String 
            
            
            picConstaint?.uninstall()
            
            
            if originalViewModel?.model?.pic_urls?.count > 0 {
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                     picConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                pictureView.hidden = false
                //传值给配图视图
                ////!!!!!!!!!!!!!!!!!!!!!
//                pictureView.pictures = originalViewModel?.model?.pic_urls
                pictureView.pictures = originalViewModel?.model?.pic_urls   
                /////////!!!!!!!!!!!!!!!!!!!!!
            }else{
                self.snp_updateConstraints(closure: { (make) -> Void in
                     picConstaint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                pictureView.hidden = true
            }
        }
    }
   override init(frame: CGRect) {
         super.init(frame: frame)
      setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
         fatalError("coder aDecoder error")
    }
    private func setupUI(){
        addSubview(photoImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(verifitiedImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top).offset(SinaHomeMargin).constraint
            make.width.equalTo(40)
            make.height.equalTo(40)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(SinaHomeMargin)
            make.top.equalTo(self.snp_top).offset(SinaHomeMargin)
        }
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(SinaHomeMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        verifitiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right).offset(-SinaHomeMargin/2)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        //时间的约束
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(SinaHomeMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(SinaHomeMargin/2)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_bottom).offset(SinaHomeMargin/2)
            make.left.equalTo(timeLabel.snp_right).offset(SinaHomeMargin)
//            make.centerY.equalTo(timeLabel.snp_centerY).offset(SinaHomeMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        //内容的约束
         contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(photoImageView.snp_bottom).offset(SinaHomeMargin)
            make.width.equalTo(UIScreen.mainScreen().bounds.width - CGFloat(SinaHomeMargin) * 2)
            
            
        }
        //添加配图的颜色
//        pictureView.backgroundColor = UIColor.redColor() 
        //给配图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(contentLabel.snp_bottom)
        }
        self.snp_makeConstraints { (make) -> Void in
            picConstaint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
        }
    }
    //懒加载
    lazy var photoImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "avatar_default_big")
        img.layer.masksToBounds = true
        img.layer.cornerRadius = 20
        
         return img
    }()
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.text = "昵称"
        label.font = UIFont.systemFontOfSize(14)
        label.textColor  = UIColor.blackColor()
        label.shadowColor = UIColor.magentaColor()
        label.shadowOffset = CGSizeMake(3, -3)
        return label
    }()
    lazy var levelImageView: UIImageView = {
    let img = UIImageView()
    img.image = UIImage(named: "common_icon_membership_level1")
        return img
    }()
    lazy var verifitiedImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "avatar_enterprise_vip")
        
        return img
    }()
    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.text = "时间"
//        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.orangeColor()
        label.shadowColor = UIColor.magentaColor()
        label.shadowOffset = CGSizeMake(3, -3)
        return label
    }()
    lazy var sourceLabel: UILabel = {
        let label = UILabel()
        label.text = "来源"
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.blackColor()
        label.numberOfLines = 0
        label.shadowColor = UIColor.magentaColor()
        label.shadowOffset = CGSizeMake(3, -3)
        return label
    }()
    lazy var contentLabel:UILabel = {
        let label = UILabel()
        label.text = "来源"
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.blackColor()
        label.numberOfLines = 0
        return label
    }()
    lazy var pictureView: SinaPictureView  = SinaPictureView()
}
